'use strict';
/**
 * Created by FreezeSoul on 2015/10/24.
 */
define([
    "app/core/base",
    "app/core/util",
    "../commonMeta",
    "video",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'jqueryTmpl',
    'jQueryUUID'
], function (Chart, Util, Meta, videojs, $, _, JSON, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '基础',
                id: 'ShiPinZiYuanLuJing',
                name: '视频资源路径',
                enable: true,
                type: 'resource',
                value: '',
                default: '',
                link: 'videoUrl',
                tooltip: '设置视频的资源路径，格式要求MP4，外部视频http作为前缀'
            },
            {
                group: '基础',
                id: 'ShiPinZiDongBoFang',
                name: '自动播放',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'autoPlay',
                tooltip: '设置视频自动播放'
            },
            {
                group: '基础',
                id: 'ShiPinJinYongYinXiao',
                name: '禁用音效',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'mute',
                tooltip: '设置视频禁用音效'
            },
            {
                group: '基础',
                id: 'ShiPinShowControl',
                name: '显示控制按钮',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'controls',
                tooltip: '设置视频显示控制按钮'
            },
            {
                group: '基础',
                id: 'ShiPinLoop',
                name: '是否循环',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'loop',
                tooltip: '设置视频是否循环'
            },
            {
                group: '基础',
                id: 'PlaybackRate',
                name: '播放速度',
                enable: false,
                type: 'number',
                value: 1,
                default: 1,
                link: 'playbackRate',
                tooltip: '播放速度'
            },
            {
                group: '基础',
                id: 'ShiPinClickControl',
                name: '点击播放/暂停',
                enable: false,
                type: 'checked',
                value: false,
                default: false,
                link: 'clickControl',
                tooltip: '设置是否支持点击视频播放/暂停'
            }
        ].concat($.extend(true, [], Meta.commonProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var image = Chart.extend({
        constructor: function (layout) {
            videojs.options.flash.swf = "libs/video.js/dist/video-js.swf";
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._videoPlayer = null;
            this._uuid = "video_" + $.uuid();
            this._template = '<video style="object-fit: fill !important;" id="${videoId}" class="video-js vjs-default-skin">' +
                '<source src="${videoUrl}" type="video/mp4" />' +
                '</video>';
            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    videoUrl: '',
                    autoPlay: true,
                    controls: true,
                    playbackRate: 1,
                    mute: false,
                    loop: true,
                    clickControl: false,
                    initScript: '',
                    disposeScript: ''
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function (option) {
            var _this = this;
            var videoUrl = option.videoUrl;
            this._uuid = "video_" + $.uuid();
            if (option.videoUrl.toLowerCase().indexOf("http") === 0) {
                videoUrl = option.videoUrl;
            } else {
                var rootPath = "static/resource/";
                videoUrl = adConfig.baseServiceUrl + rootPath + option.videoUrl;
            }
            var $element = $.tmpl(this._template, {
                videoId: _this._uuid,
                videoUrl: videoUrl
            });
            $(this._container).empty();
            if (this._videoPlayer)
                this._videoPlayer.dispose()
            $element.appendTo(this._container);
            videojs(this._uuid, {
                controls: option.controls,
                playbackRates: [0.5, 1, 1.5, 2],
                autoplay: option.autoPlay,
                muted: !!option.mute,
                loop: option.loop,
                preload: 'auto'
            }).ready(function () {
                _this._videoPlayer = this;
                this.playbackRate(option.playbackRate ? option.playbackRate : 1);
            });
            if (option.clickControl === true) {
                $element.click(function () {
                    if (_this._videoPlayer.paused() === false) {
                        _this._videoPlayer.pause();
                    } else {
                        _this._videoPlayer.play();
                    }
                });
            }
            //Util.setCommonMeta(this, this._container, this._option.Option);
            //Util.initChartLifeTime(this, this._container, this._option.Option);
            return $element;
        },
        example: function () {
            try {
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _bindData: function (data) {
        },
        setData: function (data) {
            try {
                this._readProperty();

                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    this._bindData(data);
                }
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                if (this._videoPlayer)
                    this._videoPlayer.dispose()
                $(this._element).off().remove();
                //Util.disposeChartLifeTime(this, this._container, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: image,
        Property: property
    };
});